package com.wangyb.wework.wxApi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.wangyb.wework.entity.contact.WwTag;
import com.wangyb.wework.entity.contact.WwTagGroup;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.wangyb.wework.common.WxApiConst.API_GET_CORP_TAG_LIST;

/**
 * @Author: wangyb
 * @Date: 2021/7/1 3:30 下午
 */
@Data
public class CorpTagListResp extends BaseWxResp {
	@Override
	public String reqUrl() {
		return API_GET_CORP_TAG_LIST;
	}
	@JSONField(name = "tag_group")
	private String tagGroup;

	public List<WwTagGroup> getTagGroupList(){
		List<WwTagGroup> groupList = new ArrayList<>();
		JSONArray tagGroupJsonArray = JSONArray.parseArray(this.tagGroup);
		for (int i=0; i<tagGroupJsonArray.size();i++){
			JSONObject g = tagGroupJsonArray.getJSONObject(i);
			WwTagGroup group  = new WwTagGroup();
			group.setGroupName(g.getString("group_name"));
			group.setWxGroupId(g.getString("group_id"));
			group.setOrder(g.getInteger("order"));
			group.setDeleted(g.getBoolean("deleted"));
			group.setAddTime(new Date(g.getLong("create_time") * 1000));

			List<WwTag> tagList = new ArrayList<>();
			JSONArray tagJsonArray = g.getJSONArray("tag");
			for(int j=0; j< tagJsonArray.size();j++){
				JSONObject t = tagJsonArray.getJSONObject(j);
				WwTag tag = new WwTag();
				tag.setWxTagId(t.getString("id"));
				tag.setName(t.getString("name"));
				tag.setOrder(t.getInteger("order"));
				tag.setAddTime(new Date(t.getLong("create_time") * 1000));
				tag.setDeleted(t.getBoolean("deleted"));
				tagList.add(tag);
			}
			group.setTagList(tagList);
			groupList.add(group);
		}
		return groupList;
	}
}
